package com.gwt.client;


import com.game.code.Game;
import com.game.interfacesForGame.ApplicationListener;
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.gwt.implementation.MouseHandler;
import com.gwt.implementation.Screen;

public class Loader implements EntryPoint {
	
    HTMLPanel panel;
    
    ApplicationListener game = new Game();
    
    //timer refresh rate, in milliseconds
    static final int refreshRate = 25;
    static final float EditorSizePer = 0.3f;
    
    static final String id = "contentholder";
    static final String instrumentsPanelId = "instrumentsPanelId";
    
    
    MouseHandler mouse = new MouseHandler();
    
	public void onModuleLoad() {

        panel = new HTMLPanel("<div class=\"content\" id=\"" + id + "\"></div>");
        panel.setStyleName("contentHolder");
        

        Window.addResizeHandler(new ResizeHandler() {
 
            public void onResize(ResizeEvent event)
            {
            	resizeCanvas(panel.getOffsetWidth()-1,panel.getOffsetHeight()-1);
            }
        });

        SplitLayoutPanel splitPanel = new SplitLayoutPanel(2){
            public void onResize()
            {
            	resizeCanvas(panel.getOffsetWidth()-1,panel.getOffsetHeight()-1);
            }
        };   

        HTMLPanel instrumentsPanel = new HTMLPanel("<div class=\"content\" id=\"" + instrumentsPanelId + "\"></div>");

        
        splitPanel.getElement().getStyle().setProperty("border", "0px solid #e7e7e7");
        
        RootPanel.get().add(splitPanel,0,0);
        splitPanel.setSize("100%", "100%");
        
        splitPanel.addWest(instrumentsPanel, splitPanel.getOffsetWidth()*EditorSizePer);
        splitPanel.add(panel);
        resizeCanvas((int)(splitPanel.getOffsetWidth()*(1-EditorSizePer))-1, splitPanel.getOffsetHeight()-1);
        
        
        @SuppressWarnings("unused")
		Instruments instruments = new Instruments(instrumentsPanel);

        game.create(mouse);
        
        final Timer timer = new Timer() {
            public void run() {
               // drawSomethingNew();
            }
        };
        timer.scheduleRepeating(refreshRate);
	}
	
	int i=0;
	
	public void drawSomethingNew() {
		panel.clear();
		
		i++;
		if(i%2==1){
			game.render(screen1,refreshRate);
	        panel.add(screen1.canvas, id);
		}else{
			game.render(screen2,refreshRate);
			panel.add(screen2.canvas, id);
		}
    }
	
	Screen screen1;
	Screen screen2;
	
	private void resizeCanvas(int width, int height)
    {
        width = width-2;
        height = height-2;

        game.resize(width,height);
 
        panel.clear();

        screen1 = new Screen();
        screen2 = new Screen();
        

        
        screen1.canvas = null;
        screen1.canvas = Canvas.createIfSupported();
        screen2.canvas = null;
        screen2.canvas = Canvas.createIfSupported();
 
        if (screen1.canvas == null)
        {
              RootPanel.get().add(new Label("Sorry, your browser doesn't support the HTML5 Canvas element"));
              return;
        }
        /*
 
        int size=Math.min(width, height);
        screen1.canvas.setStyleName("mainCanvas");
        screen1.canvas.setWidth(size + "px");
        screen1.canvas.setCoordinateSpaceWidth(size);
        screen1.canvas.setHeight(size + "px");
        screen1.canvas.setCoordinateSpaceHeight(size);

        screen2.canvas.setStyleName("mainCanvas");
        screen2.canvas.setWidth(size + "px");
        screen2.canvas.setCoordinateSpaceWidth(size);
        screen2.canvas.setHeight(size + "px");
        screen2.canvas.setCoordinateSpaceHeight(size);
		*/
        
        screen1.canvas.setStyleName("mainCanvas");
        screen1.canvas.setWidth(width + "px");
        screen1.canvas.setCoordinateSpaceWidth(width);
        screen1.canvas.setHeight(height + "px");
        screen1.canvas.setCoordinateSpaceHeight(height);

        screen2.canvas.setStyleName("mainCanvas");
        screen2.canvas.setWidth(width + "px");
        screen2.canvas.setCoordinateSpaceWidth(width);
        screen2.canvas.setHeight(height + "px");
        screen2.canvas.setCoordinateSpaceHeight(height);
        panel.add(screen1.canvas, id);

        screen1.canvas.setPixelSize(width, height);
        screen2.canvas.setPixelSize(width, height);
        
        screen1.canvas.addMouseMoveHandler(mouse);
        screen2.canvas.addMouseMoveHandler(mouse);
        
    }
}
